package edu.gxmzu.task.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gxmzu.task.config.security.MyUserDetails;
import edu.gxmzu.task.entity.Accounts;
import edu.gxmzu.task.entity.UserManageInfo;
import edu.gxmzu.task.mapper.UserInfoMapper;
import edu.gxmzu.task.models.UserInfoModel;
import edu.gxmzu.task.models.UserModel;
import edu.gxmzu.task.service.UserInfoService;
import edu.gxmzu.task.utils.JWTUtil;
import edu.gxmzu.task.utils.RedisService;
import edu.gxmzu.task.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private RedisService redisService;

    @Override
    public Result getUserInfoById(String userId) {
        return Result.ok("Test");
    }

    @Override
    public PageInfo<UserManageInfo> getUserManageInfo(String name) {

        // 获取分页后的数据
        List<UserManageInfo> userManageInfoList = userInfoMapper.getUserManageInfo(name);

        // 使用 PageInfo 包装查询结果和分页信息
        return new PageInfo<>(userManageInfoList);
    }

    @Override
    public int updatePassword(String userId, String password) {
        String encodePassword = passwordEncoder.encode(password);
        int i = userInfoMapper.updatePassword(userId, encodePassword);
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Accounts accounts = (Accounts) authenticationToken.getPrincipal();
        // 删除缓存信息
        if (userId.equals(accounts.getUserId())){
            redisService.deleteValue(RedisService.USER_KEY + accounts.getUsername());
        }
        return i;
    }

    @Override
    public int editUserInfo(UserManageInfo userManageInfo) {
        return userInfoMapper.editUserInfo(userManageInfo);
    }

    @Override
    public UserInfoModel UserInfo(String userId) {
        return userInfoMapper.UserInfo(userId);
    }

    @Override
    public int UpdateUserInfoById(UserInfoModel userInfoModel) {
     //   log.info("密码："+userInfoModel.getPassword());

        boolean flag = false;
        if(!(userInfoModel.getPassword()==null)&&!userInfoModel.getPassword().isEmpty()) {
            String  encodePassword = passwordEncoder.encode(userInfoModel.getPassword());
            userInfoModel.setPassword(encodePassword);
            flag = true;
        }
        else {
            userInfoModel.setPassword(null);
        }
        int i = userInfoMapper.updateAccounts(userInfoModel);
        int i1 = userInfoMapper.updateUserInfo(userInfoModel);
        // 删除缓存信息
        if (flag){

            UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            Accounts accounts = (Accounts) authenticationToken.getPrincipal();
            redisService.deleteValue(RedisService.USER_KEY + accounts.getUsername());
        }

        return i;
    }

}
